<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 
*/
class Order extends MY_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('User/UserCardModel','order');
	}

	//列表页
	public function index()
	{
		//获取场馆
		//$this->load->model('User/CoachModel','coach');
		//$data['venue'] = $this->coach->get_venue_list();


		$list = $this->order->get_list_data();
		$data['coach_list'] = $list['list'];

		$this->load->view('order_list', $data);
	}


	//获取列表数据
	public function get_list()
	{
		$page = $this->input->get('page');
		$page = $page ? $page : 1;
	
		$term['keyword'] = $this->input->get('keyword');
		$term['date'] = $this->input->get('date');
		
		
		
		$res = $this->order->get_list_data($term,$page);

		if ($res) 
		{
			$data = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();
	}


	//添加、编辑页
	public function get_info()
	{
		$data['id'] = intval($this->uri->segment(3));
		//编辑
		if ($data['id'] > 0) 
		{
			$data = $this->order->get_info($data['id']);			
		}

		$this->load->view('order_info', $data);
	}


	//提交入库
	public function insert()
	{
		$field = array('theme_img','type_id','name','title','shop_id','cid','nandu','ranzhi','duration','info','content','content','status','suitable','max_num');
		$arr = $this->input->post($field, TRUE);

		$apellation = $this->input->post('apellation', TRUE);
		$desc = $this->input->post('desc', TRUE);

		$arr['sport'] = !empty($_POST['sport']) ? join(',',$_POST['sport']) : '';
		
		//处理数据
		$sui_arr = explode('|',$arr['suitable']);
		$suitable = array();
		for ($i=0; $i < count($sui_arr); $i++) { 
			array_push($suitable, $sui_arr[$i]);
		}
		$arr['suitable'] = json_encode($suitable);
		$tool = array('title'=>$apellation,'info'=>$desc);
		$arr['tool'] = json_encode($tool);

		$course_id = $this->input->post('course_id');

		//添加
		if (empty($course_id)) 
		{
			$res = $this->courses->inser_course_data($arr);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '添加成功！';

			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '添加失败！';
			}
		}
		//更新
		else
		{
			$res = $this->courses->updata_course_data($arr,$course_id);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '更新成功！';

			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '更新失败！';
			}

		}

		echo json_encode($data);exit();

	}


	//删除
	public function del()
	{
		$course_id = $this->input->get('id', true);

		//$res = $this->courses->remove_course($course_id);
		
		$res = $this->courses->updata_course_data(array("status"=>0),$course_id);
		
		if ($res){
			$data['status'] = 200;
			$data['message'] = '删除成功！';
		}
		else{
			$data['status'] = 400;
			$data['message'] = '删除失败！';
		}

		echo json_encode($data);exit();

	}




}



?>